$(function () {
	initProfitCost = 1;
	initProfitCostData();

});

//界面数据初始化
function initProfitCostData() {
	//请求参数
	var obj = {
		budgetYear: fullYear,
		budgetMonth: fullMonth,
		dept: fullDepartment,
		status: 2,
		chartPage: '5010302'
	}

	//API
	ApiGet('getdata', obj, function (e) {
		var json = eval('('+ e +')')
		var chartData = json.items[0].charts;
		setPanel(chartData)
		getcashflowtable(chartData)
	});

}

//设置面板
function setPanel(data) {
	var alldata = data;
	// console.log(alldata[1],'-----------');
	var num1 = alldata[0]['data'][0]['key01'];
	var num2 = alldata[0]['data'][0]['key02'];
	var num3 = alldata[0]['data'][0]['key03'];
	num1 = parseFloat(num1)
	num2 = parseFloat(num2)
	num3 = parseFloat(num3)

	function format(num) {
		undefined
		return (num.toFixed(2) + '').replace(/\d{1,3}(?=(\d{3})+(\.\d*)?$)/g, '$&,');
	}
	$(".implementmoney1").text(format(num1));
	$(".implementmoney2").text(format(num2));
	$(".implementmoney3").text(format(num3));

}

function getcashflowtable(data) {
	var alldata = data;
	var thisdata = alldata[1]['data']

	var tbody = document.getElementById('ttbody');


	for (let i = 0; i < thisdata.length; i++) {
		num1 = parseFloat(thisdata[i].key03)
		if (isNaN(num1)) {
			num1 = 0;
		}
		num2 = parseFloat(thisdata[i].key04)
		if (isNaN(num2)) {
			num2 = 0;
		}
		num3 = parseFloat(thisdata[i].key05)
		function format(num) {
			return (num.toFixed(2) + '').replace(/\d{1,3}(?=(\d{3})+(\.\d*)?$)/g, '$&,');
		}

		var trobj = document.createElement("tr");
		var thobj1 = document.createElement("th");
		thobj1.innerHTML = (thisdata[i].key01).replace(' ', '&nbsp;');
		if (thisdata[i].key06 == 1) {
			thobj1.className = "th1";
		} else {
			thobj1.className = "th2";
		}

		var thobj2 = document.createElement("th");
		thobj2.innerHTML = format(num1);
		if (thisdata[i].key06 == 1) {
			thobj2.className = "th1";
		} else {
			thobj2.className = "th2";
		}


		var thobj3 = document.createElement("th");
		thobj3.innerHTML = format(num2);
		if (thisdata[i].key06 == 1) {
			thobj3.className = "th1";
		} else {
			thobj3.className = "th2";
		}


		var thobj4 = document.createElement("th");
		if (thisdata[i].key06 == 1) {
			thobj4.className = "th1";
		} else {
			thobj4.className = "th2";
		}

		if (num3 > 0) {
			thobj4.innerHTML = "<a class='trup'></a><span class='contrastnumberup'>" + format(num3) + "</span>";
		}
		if (num3 < 0) {
			thobj4.innerHTML = "<a class='trdown'></a><span class='contrastnumberdown'>" + format(num3).slice(1) + "</span>";
		}
		if (num3 == 0) {
			thobj4.innerHTML = "<span class='contrastnumbersame'>---</span>";
		}

		trobj.appendChild(thobj1);
		trobj.appendChild(thobj2);
		trobj.appendChild(thobj3);
		trobj.appendChild(thobj4);
		tbody.appendChild(trobj);

	}
}

